﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Persister;
using Kaidou.Model;
using Kaidou.Util;


public partial class seal_seal_order_delete : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];
        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        try
        {

            Order order = (Order)Session["order"];
            TRN_Juchu_HPersister parH = new TRN_Juchu_HPersister();
            TRN_Juchu_DPersister parD = new TRN_Juchu_DPersister();
            TrnJuchuh h = order.getJuchu_H();
            TrnJuchud[] d = order.getJuchu_D();
            MST_UnionPersister dao1 = new MST_UnionPersister();
            MST_Union_BranchPersister dao2 = new MST_Union_BranchPersister();
            MstUnion union = dao1.getDetail(h.UnionCode);
            mst_union_branch branch = dao2.getDetail(h.UnionCode, h.BranchCode);


            if (TrnJuchuh.JUCHU_CLSS_INTERNET.Equals(h.JuchuClss))
            {
                htitle.Text = "インターネット";
            }
            else if (TrnJuchuh.JUCHU_CLSS_FAX.Equals(h.JuchuClss))
            {
                htitle.Text = "FAX/OCR";
            }

            JUCHU_DATE.Text = ((DateTime)h.JuchuDate).ToString("yyyy年MM月dd日");

            MST_UNION_CODE.Text = h.UnionCode;
            ParentOfficeName.Text = union == null ? "" : union.OfficeName;
            MST_BRANCH_CODE.Text = h.BranchCode == 0 ? "000" : h.BranchCode.ToString();
            Branch_office_name.Text = branch == null ? "本店" : branch.officename;
            TRN_JUCHU_TANTO_NAME.Text = h.JuchuTantoName;

            if (h.HopeDate != null)
            {
                DateTime dt = (DateTime)h.HopeDate;
                TRN_HOPE_DATE.Text = dt.ToString("yyyy年MM月dd日");

            }
            else
            {
                TRN_HOPE_DATE.Text = "指定なし";
            }

            if (TrnJuchuh.HOPE_TIME_CLSS_NONE.Equals(h.HopeTimeClss))
            {
                TRN_HOPE_TIME_CLSS.Text = "指定なし";
            }

            if (TrnJuchuh.HOPE_TIME_CLSS_MORNING.Equals(h.HopeTimeClss))
            {
                TRN_HOPE_TIME_CLSS.Text = "午前";
            }

            if (TrnJuchuh.HOPE_TIME_CLSS_AFTERNOON.Equals(h.HopeTimeClss))
            {
                TRN_HOPE_TIME_CLSS.Text = "午後";
            }

            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');


            if (branch != null)
            {
               
                addressmsg.Text = "〒" + branch.officezipno.Substring(0, 3) + "-" + branch.officezipno.Substring(3, 4) + "<br/>" + areaArray[branch.officeprefcode-1] + " " + branch.officecity + " " + branch.officeaddress + "<br/>" +
                    "TEL：" + branch.officetel + " FAX：" + branch.officefax;

            }
            else if (h.BranchCode == 0 && union != null)
            {
                addressmsg.Text = "〒" + union.OfficeZipNo.Substring(0, 3) + "-" + union.OfficeZipNo.Substring(3, 4) + "<br/>" + areaArray[union.OfficePrefCode-1] + " " + union.OfficeCity + " " + union.OfficeAddress + "<br/>" +
                    "TEL：" + union.OfficeTel + " FAX：" + union.OfficeFax;
            }
            else
            {
                addressmsg.Text = "組合員が見つかりません。";
            }

            TRN_SEAL_QTY1.Text = d[0] == null ? "" : d[0].SealQty.ToString();

            Kaidou.Persister.MST_TraderPersister traderDao = new Kaidou.Persister.MST_TraderPersister();
            MstTrader trader = null;

            if (d[1] != null)
            {
                trader = traderDao.getDetail(d[1].TraderRegNo);
                TRADER_REG_NO2.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                TRADER_REG_NO_LABEL2.Text = trader != null ? trader.TraderName : "";
                TRN_SEAL_QTY2.Text = d[1] != null ? d[1].SealQty.ToString() : "";
            }

            if (d[2] != null)
            {
                trader = traderDao.getDetail(d[2].TraderRegNo);
                TRADER_REG_NO3.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                TRADER_REG_NO_LABEL3.Text = trader != null ? trader.TraderName : "";
                TRN_SEAL_QTY3.Text = d[2] != null ? d[2].SealQty.ToString() : "";
            }

            if (d[3] != null)
            {
                trader = traderDao.getDetail(d[3].TraderRegNo);
                TRADER_REG_NO4.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                TRADER_REG_NO_LABEL4.Text = trader != null ? trader.TraderName : "";
                TRN_SEAL_QTY4.Text = d[3] != null ? d[3].SealQty.ToString() : "";
            }

            if (d[4] != null)
            {
                trader = traderDao.getDetail(d[4].TraderRegNo);
                TRADER_REG_NO5.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) + " - " + trader.TraderRegNo.Substring(2, 5) : "";
                TRADER_REG_NO_LABEL5.Text = trader != null ? trader.TraderName : "";
                TRN_SEAL_QTY5.Text = d[4] != null ? d[4].SealQty.ToString() : "";
            }

            sum.Text = h.TotalSealQty.ToString();

        }
        catch (Exception ex)
        {

            LogManager.error("seal_seal_order_detail " + ex.Message + ex.StackTrace);
        }
    }

    protected void update(object sender, EventArgs e)
    {
        try {
			 Order order = (Order)Session["order"];
            LoginModel loginModel = (LoginModel)Session["loginModel"];
			//パーシスターを作成
			TRN_Juchu_HPersister perH = new TRN_Juchu_HPersister();

			TrnJuchuh h = order.getJuchu_H();

			h.JuchuSts = TrnJuchuh.JUCHU_STATUS_DELETE;
			h.UpdDate =System.DateTime.Now;
			h.UpdUnionCode =loginModel.Unioncode;
			h.UpdBranchCode = (short)loginModel.Branchcode;
			h.UpdFuncId ="deleteOrderForKU";

			//update
			perH.updateJuchu_HForKaidouUser(new SqlDB(), h );

			
		} catch (Exception pe) {
			LogManager.error("TRN_JuchuManager updateOrderForKaidouUser :"+pe.Message+pe.StackTrace);
			
		}

        Session["back"] = 1;
        Response.Redirect("seal_order_search.aspx");
    }
}
